package com.kaishengit.util;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFUtil {

	public static ByteArrayOutputStream createpdf(com.kaishengit.pojo.Document document){
		
		try {
			Document doc = new Document();
			
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			PdfWriter.getInstance(doc, bout);
			
			doc.addTitle(new String(document.getTitle().getBytes("gb2312"),"ISO8859-1"));
					
			doc.open();
			
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font fontChinese = new Font(bfChinese,12,Font.NORMAL);
			Paragraph para = new Paragraph(document.getContent(), fontChinese);
			
			doc.add(para);
			
			doc.close();
			
			return bout;
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
		
		return null;
		
		
	}
}
